package com.kou.dao;

import com.kou.pojo.Goods;
import com.kou.utils.DataSourceUtil;
import com.kou.utils.StrUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author SteinY
 * @create 2022-03-25 15:22
 * @description:
 */
public class GoodsDaoImpl implements GoodsDao {
    QueryRunner queryRunner = new QueryRunner();
    @Override
    public List<Goods> findGoodslist(String tid, String currentPage, int pageSize) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = " select * from tb_goods where tid = ? limit ?,? ";
        Object params[] = {tid,(Integer.parseInt(currentPage)-1)*pageSize,pageSize};
        try {
            return queryRunner.query(connection,sql,new BeanListHandler<>(Goods.class),params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Long findGoodsCount(String tid) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "select count(*) from tb_goods where tid = ? ";
        try {
            Object query = queryRunner.query(connection, sql, new ScalarHandler(), tid);
            //1:Long 长整数 特殊类型
            // Object --> Long 强转异常
            // Object --> Integer --> Long
            // Object --> String  --> Long
            return Long.parseLong(String.valueOf(query));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Goods findGoodsById(String id) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = " select * from tb_goods where id = ? ";
        //参数
        Object[] params = {id};
        try {
            return queryRunner.query(connection,sql, new BeanHandler<Goods>(Goods.class), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Goods> select(String input, String currentPage, int pageSize) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "SELECT * FROM tb_goods WHERE NAME LIKE '%"+input+"%' LIMIT ?,?";
        Object[] params = {(Integer.parseInt(currentPage)-1)*pageSize,pageSize};
        try {
            return queryRunner.query(connection,sql,new BeanListHandler<>(Goods.class),params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Long selectCount(String input) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "SELECT count(*) FROM tb_goods WHERE NAME LIKE '%"+input+"%'";

        try {
            Object query = queryRunner.query(connection, sql, new ScalarHandler());
            return Long.parseLong(String.valueOf(query));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Goods> findAll() {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "SELECT * from tb_goods";
        try {
            return queryRunner.query(connection,sql,new BeanListHandler<>(Goods.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void create(Goods goods) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "insert into tb_goods values (0,?,?,?,?,?,?,?)";
        Object params[] = {goods.getName(),goods.getPubdate(),goods.getPicture(),goods.getPrice(),goods.getStar(),goods.getIntro(),goods.getTid()};
        try {
            queryRunner.update(connection,sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void delete(String id) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "delete from tb_goods where id = ? ";
        try {
            queryRunner.update(connection,sql,Integer.parseInt(id));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Goods> search(String name, String price) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "select * from tb_goods where price = ? and name like  '%"+name+"%'";
        try {
           return queryRunner.query(connection,sql,new BeanListHandler<>(Goods.class),price);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void update(Goods good) {
        Connection connection = DataSourceUtil.getConnection();
        try {
            if (StrUtils.empty(good.getPicture())){
                String sql = "update tb_goods set name = ?,pubdate = ?,picture = ?,price = ?,star = ?, intro = ?,tid = ? where id = ?";
                Object params[] = {good.getName(),good.getPubdate(),good.getPicture(),good.getPrice(),good.getStar(),good.getIntro(),good.getTid(),good.getId()};
                queryRunner.update(connection,sql,params);
            }else {
                String sql = "update tb_goods set name = ?,pubdate = ?,price = ?,star = ?, intro = ?,tid = ? where id = ?";
                Object params[] = {good.getName(),good.getPubdate(),good.getPrice(),good.getStar(),good.getIntro(),good.getTid(),good.getId()};
                queryRunner.update(connection,sql,params);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
